package com.test.xq.customdemo.customtools;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.util.AttributeSet;
import android.view.View;

/**
 * Created by xq on 17/4/12.
 */


//Canvas之绘制图形
public class CustomView1 extends View {

    private Paint mPaint = new Paint();

    public CustomView1(Context context) {
        super(context);
    }

    public CustomView1(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        initPaint();
    }

    //初始化画笔
    private void initPaint() {
        mPaint.setColor(Color.BLACK); //设置画笔颜色
        mPaint.setStyle(Paint.Style.FILL); //设置画笔模式为填充
        mPaint.setStrokeWidth(10f); //设置画笔的宽度为10px
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//        如果对View的宽高进行修改了，不要调用 super.onMeasure( widthMeasureSpec, heightMeasureSpec);
//        要调用 setMeasuredDimension( widthsize, heightsize); 这个函数。
//        setMeasuredDimension(widthMeasureSpec,heightMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int wifthMode = MeasureSpec.getMode(widthMeasureSpec);

        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
    }

    private void drawPoint(Canvas canvas) {
        //在坐标（200，200）位置绘制一个点
        canvas.drawPoint(200, 200, mPaint);
        //绘制一组点
        canvas.drawPoints(new float[]{
                500, 500,
                500, 600,
                500, 700
        }, mPaint);
    }

    private void drawLine(Canvas canvas) {
        // 在坐标(300,300)(500,600)之间绘制一条直线
        canvas.drawLine(300, 300, 500, 600, mPaint);
        // 绘制一组线 每四数字(两个点的坐标)确定一条线
        canvas.drawLines(new float[]{
                100, 200, 200, 200,
                100, 300, 200, 300
        }, mPaint);
    }

    private void drawRect(Canvas canvas) {
        //第一种就是提供四个数值(矩形左上角和右下角两个点的坐标)来确定一个矩形进行绘制
        canvas.drawRect(100, 100, 800, 400, mPaint);
        //其余两种是先将矩形封装为Rect或RectF(实际上仍然是用两个坐标点来确定的矩形)，然后传递给Canvas绘制
        //两者最大的区别就是精度不同，Rect是int(整形)的，而RectF是float(单精度浮点型)的
        Rect rect = new Rect(100, 100, 800, 400);
        canvas.drawRect(rect, mPaint);

        RectF rectF = new RectF(100, 100, 800, 400);
        canvas.drawRect(rectF, mPaint);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    private void drawRoundRect(Canvas canvas) {
        //绘制圆角矩形  上面两种方法绘制效果也是一样的，但鉴于第二种方法在API21的时候才添加上，所以我们一般使用的都是第一种。
        // 第一种  30,30  这里圆角矩形的角实际上不是一个正圆的圆弧，而是椭圆的圆弧，这里的两个参数实际上是椭圆的两个半径
        RectF rectF1 = new RectF(100, 100, 600, 400);
        canvas.drawRoundRect(rectF1, 30, 30, mPaint);
        // 第二种
        canvas.drawRoundRect(100, 100, 600, 400, 30, 30, mPaint);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    private void drawOval(Canvas canvas) {
        //绘制椭圆  第一种
        RectF rectF = new RectF(100, 100, 800, 400);
        canvas.drawOval(rectF, mPaint);

        //第二种
        canvas.drawOval(100, 100, 800, 400, mPaint);
    }

    private void drawCircle(Canvas canvas) {
        //绘制圆
        canvas.drawCircle(500, 500, 400, mPaint);
    }

    private void drawArc(Canvas canvas) {
        //绘制圆弧
        RectF rectF = new RectF(100, 100, 800, 400);
        //绘制背景矩形
        mPaint.setColor(Color.GRAY);
        canvas.drawRect(rectF, mPaint);

        //不使用中心绘制圆弧
        mPaint.setColor(Color.BLUE);
        canvas.drawArc(rectF, 0, 90, false, mPaint);

        //-------------------------------------

        RectF rectF2 = new RectF(100, 600, 800, 900);
        // 绘制背景矩形
        mPaint.setColor(Color.GRAY);
        canvas.drawRect(rectF2, mPaint);

        // 使用中心绘制圆弧
        mPaint.setColor(Color.BLUE);
        canvas.drawArc(rectF2, 0, 90, true, mPaint);
    }
}
